<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Response;
/**
 * 前后端统一格式的资源类
 */
class Result extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
         return [
            'code' => $this->code,
            'msg'  => $this->msg,
            'data' => $this->data
        ];
    }

    private $code;
    private $msg;
    private $data;

    public function __construct($code, $msg, $data)
    {
        $this->code = $code;
        $this->msg = $msg;
        $this->data = $data;
    }

    public static function OK($msg, $data=null)
    {
        return new self(200, $msg, $data);
    }

    public static function FAIL($code, $msg, $data=null)
    {
        return new self($code, $msg, $data);
    }
}
?>
